
(function (window, undefined) {

    var index;
    //扩展date格式化功能
    Date.prototype.format = function (format) {
        var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        var o = {
            "M+": this.getMonth() + 1, //month
            "d+": this.getDate(), //day
            "h+": this.getHours(), //hour
            "m+": this.getMinutes(), //minute
            "s+": this.getSeconds(), //second
            "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
            "S": this.getMilliseconds(), //millisecond
            "w": weekDay[this.getDay()] //week
        }

        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }

        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    };

    var ajaxDefault = {
        beforeSend: function (xhr) {
            console.log(xhr);
        },
        complete: function (xhr) {
            console.log(xhr);
        },
        data: {},
        success: function (data, textStatus, jqXHR) {
            if (data == undefined) {
                alert('返回结果错误');
            }
            if (data.code == code) {
                alert('调用成功');
            }
        },
        error: function (xhr, error, exception) {
            console.log(xhr);
            console.log(error);
            console.log(exception);
            alert('调用错误');
        }
    };

    function fnBeforeSend(xhr, callback) {
        index = layer.load();
        (callback || ajaxDefault.beforeSend)(xhr);
    }

    function fnAjaxSuccess(data, textStatus, jqXHR, callback) {
        //后台
        (callback || ajaxDefault.complete)(data, textStatus, jqXHR);
    }

    function fnComplete(data, textStatus, callback) {
        layer.close(index);
        (callback || ajaxDefault.complete)(data, textStatus);
    }

    window.utils = {
        noop:function(){},
        isNoValue: function (val, isNotDebarSpace) {
            if (!isNotDebarSpace && Object(val)==='string') {
                val = val.replace(/(^\s*)|(\s*$)/g, "");
            }
            return val==undefined || val == null || val === '';
        },
        alert: function (msg) {//消息,回调
            var tip = '<div class="modal fade " id="alter" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
                '<div class="modal-dialog">' +
                '<div class="modal-content">' +
                '<div class="modal-header">' +
                '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">' +
                '&times;' +
                '</button>' +
                '<h4 class="modal-title" id="myModalLabel">系统提示</h4>' +
                '</div>' +
                '<div class="modal-body">'+msg+'</div>' +
                '<div class="modal-footer">' +
                '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';
            var $modal = $(tip).modal();
        },
        confirm: function (msg, callback) {
            var confirm = '<div class="modal fade " id="alter" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
                '<div class="modal-dialog">' +
                '<div class="modal-content">' +
                '<div class="modal-header">' +
                '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">' +
                '&times;' +
                '</button>' +
                '<h4 class="modal-title" id="myModalLabel">系统提示</h4>' +
                '</div>' +
                '<div class="modal-body">'+msg+'</div>' +
                '<div class="modal-footer">' +
                '<button type="button" class="btn btn-default" data-dismiss="modal">取消</button><button type="button" id="ok" class="btn btn-primary">确认</button>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';

            var modal =  $(confirm);

            var $modal =modal.modal();

            modal.find('#ok').bind('click',function(){
                $modal.modal('hide');
                (callback||this.noop)();
            });
        },
        tip: function (msg, time, callBackFun) {
            if(this.isNoValue(msg)){
                msg = '操作成功';
            }
            var tip = '<div class="modal fade " id="alter" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
                '<div class="modal-dialog">' +
                '<div class="modal-content">' +
                '<div class="modal-header">' +
                '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">' +
                '&times;' +
                '</button>' +
                '<h4 class="modal-title" id="myModalLabel">系统提示</h4>' +
                '</div>' +
                '<div class="modal-body">'+msg+'</div>' +
                '<div class="modal-footer">' +
                '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';
            if (this.isNoValue(time)) {
                time = 2;
            }

            var $modal = $(tip).modal();

            $modal.on('hidden.bs.modal', function(){
                (callBackFun||this.noop)();
            });

            setTimeout(function(){
                $modal.modal('hide');
            },time*1000);


        },
        equal:function(one, two){
            if(one!=undefined&&one==two){
                return true;
            }
            return false;
        },
        post: function (obj) {
            var paramObj = {};
            jQuery.extend(paramObj, obj)
            if (window.jQuery == undefined) {
                alert('请引入jQuery');
            }
            var finalData = {};
            jQuery.extend(finalData, paramObj.data);
            $.ajax({
                type: 'POST',
                url: paramObj.url,
                data: finalData,
                beforeSend: function (xhr) {
                    fnBeforeSend(xhr, paramObj.beforeSend);
                },
                complete: function (xhr) {
                    fnComplete(xhr, paramObj.complete);
                },
                success: function (data, textStatus, jqXHR) {
                    fnAjaxSuccess(data, textStatus, jqXHR, paramObj.success);
                }
            });
        },
        get: function (obj) {
            var paramObj = {};
            jQuery.extend(paramObj, obj)
            if (window.jQuery == undefined) {
                alert('请引入jQuery');
            }
            var finalData = {};
            jQuery.extend(finalData, paramObj.data);
            $.ajax({
                type: 'GET',
                url: paramObj.url,
                data: finalData,
                beforeSend: function (xhr) {
                    fnBeforeSend(xhr, paramObj.beforeSend);
                },
                complete: function (xhr) {
                    fnComplete(xhr, paramObj.complete);
                },
                dataType: paramObj.dataType || ajaxDefault.dataType,
                success: function (data, textStatus, jqXHR) {
                    fnAjaxSuccess(data, textStatus, jqXHR, paramObj.success);
                }
            });
        }
    };
})(window);





